package cc.wanforme.authtest.config.auth;

import java.lang.annotation.Annotation;

import org.springframework.aop.Pointcut;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.Pointcuts;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

/** 权限验证中的工具类，代码全都来自 spring 内部
 */
public class AuthUtil {

	/**
	 * @param annotations
	 * @see {@link org.springframework.security.authorization.method.AuthorizationMethodPointcuts#forAnnotations}
	 * @return
	 */
	@SafeVarargs
	public static Pointcut forAnnotations(Class<? extends Annotation>... annotations) {
		ComposablePointcut pointcut = null;
		for (Class<? extends Annotation> annotation : annotations) {
			if (pointcut == null) {
				pointcut = new ComposablePointcut(classOrMethod(annotation));
			}
			else {
				pointcut.union(classOrMethod(annotation));
			}
		}
		return pointcut;
	}

	/**
	 * @param annotation
	 * @see {@link org.springframework.security.authorization.method.AuthorizationMethodPointcuts#classOrMethod}
	 * @return
	 */
	private static Pointcut classOrMethod(Class<? extends Annotation> annotation) {
		return Pointcuts.union(new AnnotationMatchingPointcut(null, annotation, true),
				new AnnotationMatchingPointcut(annotation, true));
	}
	
}
